package com.bing.beike.common.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;

import java.time.LocalDateTime;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class CommonResult<T> {
    private LocalDateTime timestamp;
    private Integer code;
    private String message;
    private T data;

    // 成功响应（带数据）
    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.setTimestamp(LocalDateTime.now());
        result.setCode(200);
        result.setMessage("Success");
        result.setData(data);
        return result;
    }

    // 成功响应（无数据）
    public static <T> CommonResult<T> success() {
        return success(null);
    }

    // 错误响应
    public static <T> CommonResult<T> error(Integer code, String message) {
        CommonResult<T> result = new CommonResult<>();
        result.setTimestamp(LocalDateTime.now());
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
}
